package com.dynamixsoftware.printershare;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.CharArrayBuffer;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AlphabetIndexer;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import com.dynamixsoftware.printershare.ActivityPrintContacts;
import com.dynamixsoftware.printershare.ActivityRoot;
import com.dynamixsoftware.printershare.App;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;

public class ActivityContacts extends ActivityRoot {

	private static final String FOCUS_KEY = "focused";
	private static final String LIST_STATE_KEY = "liststate";
	private static final int QUERY_TOKEN = 42;
	private static ExecutorService sImageFetchThreadPool;
	private ActivityContacts.Impl impl = ActivityContacts.Impl.getImpl();
	private ActivityContacts.ContactItemListAdapter mAdapter;
	private boolean mJustCreated;
	private boolean mListHasFocus;
	private Parcelable mListState = null;
	private ListView mListView;
	private ActivityContacts.QueryHandler mQueryHandler;
	private int mScrollState;

	// $FF: synthetic method
	static ExecutorService access$1200() {
		return sImageFetchThreadPool;
	}

	// $FF: synthetic method
	static ExecutorService access$1202(ExecutorService var0) {
		sImageFetchThreadPool = var0;
		return var0;
	}

	private Uri getContactFilterUri(String var1) {
		return !TextUtils.isEmpty(var1) ? Uri.withAppendedPath(ActivityContacts.Impl.CONTACTS_CONTENT_FILTER_URI,
				Uri.encode(var1)) : ActivityContacts.Impl.CONTACTS_CONTENT_URI;
	}

	private String getSortOrder(String[] var1) {
		return ActivityContacts.Impl.NAME_COLUMN + " COLLATE LOCALIZED ASC";
	}

	Cursor doFilter(String var1) {
		ContentResolver var2 = this.getContentResolver();
		String[] var3 = this.getProjectionForQuery();
		return var2.query(this.getContactFilterUri(var1), var3, ActivityContacts.Impl.CONTACTS_SELECTION,
				(String[]) null, this.getSortOrder(var3));
	}

	Cursor getItemForView(View var1) {
		int var2 = this.mListView.getPositionForView(var1);
		return var2 < 0 ? null : (Cursor) this.mListView.getAdapter().getItem(var2);
	}

	String[] getProjectionForQuery() {
		return ActivityContacts.Impl.CONTACTS_PROJECTION;
	}

	Uri getUriToQuery() {
		return ActivityContacts.Impl.CONTACTS_CONTENT_URI;
	}

	protected void onCreate(Bundle var1) {
		super.onCreate(var1);
		this.setContentView(2130903043);
		this.setTitle(2131099680);
		((Button) this.findViewById(2131230726)).setOnClickListener(new OnClickListener() {
			public void onClick(View var1) {
				SparseBooleanArray var2 = ActivityContacts.this.mListView.getCheckedItemPositions();
				String var3 = "";

				for (int var4 = 0; var4 < var2.size(); ++var4) {
					if (var2.valueAt(var4)) {
						Cursor var8 = ActivityContacts.this.mAdapter.getCursor();
						var8.moveToPosition(var2.keyAt(var4));
						StringBuilder var10 = (new StringBuilder()).append(var3);
						String var11;
						if (var3.length() > 0) {
							var11 = ",";
						} else {
							var11 = "";
						}

						var3 = var10.append(var11).append(var8.getString(0)).toString();
					}
				}

				if (var3.length() > 0) {
					Intent var5 = new Intent();
					var5.setClass(ActivityContacts.this, ActivityPrintContacts.class);
					var5.putExtra("data", var3);
					ActivityContacts.this.startActivity(var5);
				} else {
					Toast.makeText(ActivityContacts.this, 2131099683, 1).show();
				}
			}
		});
		this.mListView = (ListView) this.findViewById(2131230725);
		this.mListView.setChoiceMode(2);
		this.mListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView var1, View var2, int var3, long var4) {
				View var6;
				if (var2 != null) {
					var6 = var2.findViewById(2131230734);
				} else {
					var6 = null;
				}

				if (var6 != null) {
					byte var7;
					if (ActivityContacts.this.mListView.isItemChecked(var3)) {
						var7 = 0;
					} else {
						var7 = 8;
					}

					var6.setVisibility(var7);
				}

			}
		});
		this.mListView.setDividerHeight(0);
		this.mListView.setFocusable(true);
		this.mListView.setOnCreateContextMenuListener(this);
		this.mAdapter = new ActivityContacts.ContactItemListAdapter(this);
		this.mListView.setAdapter(this.mAdapter);
		this.mListView.setOnScrollListener(this.mAdapter);
		TextView var2 = (TextView) this.findViewById(2131230731);
		var2.setText(2131099812);
		var2.setGravity(17);
		this.mListView.setSaveEnabled(false);
		this.mQueryHandler = new ActivityContacts.QueryHandler(this);
		this.mJustCreated = true;
	}

	public boolean onCreateOptionsMenu(Menu var1) {
		var1.add(0, 1, 0, 2131099661);
		var1.add(0, 2, 0, 2131099662);
		return true;
	}

	public boolean onMenuItemSelected(int var1, MenuItem var2) {
		boolean var3 = true;
		switch (var2.getItemId()) {
		case 1:
			for (int var5 = 0; var5 < this.mListView.getAdapter().getCount(); ++var5) {
				this.mListView.setItemChecked(var5, var3);
			}

			return var3;
		case 2:
			for (int var4 = 0; var4 < this.mListView.getAdapter().getCount(); ++var4) {
				this.mListView.setItemChecked(var4, false);
			}

			return var3;
		default:
			var3 = false;
			return var3;
		}
	}

	protected void onRestart() {
		super.onRestart();
		if (TextUtils.isEmpty(this.mListView.getTextFilter())) {
			this.startQuery();
		} else {
			((ActivityContacts.ContactItemListAdapter) this.mListView.getAdapter()).onContentChanged();
		}
	}

	protected void onRestoreInstanceState(Bundle var1) {
		super.onRestoreInstanceState(var1);
		this.mListState = var1.getParcelable("liststate");
		this.mListHasFocus = var1.getBoolean("focused");
	}

	protected void onResume() {
		super.onResume();
		if (this.mAdapter.mBitmapCache != null) {
			this.mAdapter.mBitmapCache.clear();
		}

		this.mScrollState = 0;
		if (this.mJustCreated && true) {
			this.startQuery();
		}

		this.mJustCreated = false;
	}

	protected void onSaveInstanceState(Bundle var1) {
		super.onSaveInstanceState(var1);
		var1.putParcelable("liststate", this.mListView.onSaveInstanceState());
		var1.putBoolean("focused", this.mListView.hasFocus());
	}

	protected void onStop() {
		super.onStop();
		this.mAdapter.setLoading(true);
		this.mAdapter.changeCursor((Cursor) null);
		this.mAdapter.clearImageFetching();
	}

	void startQuery() {
		this.mAdapter.setLoading(true);
		this.mQueryHandler.cancelOperation(42);
		String[] var1 = this.getProjectionForQuery();
		Uri var2 = this.getUriToQuery();
		this.mQueryHandler.startQuery(42, (Object) null, var2, var1, ActivityContacts.Impl.CONTACTS_SELECTION,
				(String[]) null, this.getSortOrder(var1));
	}

	private static class QueryHandler extends AsyncQueryHandler {

		protected final WeakReference mActivity;

		public QueryHandler(Context var1) {
			super(var1.getContentResolver());
			this.mActivity = new WeakReference((ActivityContacts) var1);
		}

		protected void onQueryComplete(int var1, Object var2, Cursor var3) {
			ActivityContacts var4 = (ActivityContacts) this.mActivity.get();
			if (var4 != null && !var4.isFinishing()) {
				var4.mAdapter.setLoading(false);
				var4.mListView.clearTextFilter();
				var4.mAdapter.changeCursor(var3);
				if (var4.mListState != null) {
					var4.mListView.onRestoreInstanceState(var4.mListState);
					if (var4.mListHasFocus) {
						var4.mListView.requestFocus();
					}

					var4.mListHasFocus = false;
					var4.mListState = null;
				}
			} else {
				var3.close();
			}

			ListView var5 = var4.mListView;
			boolean var6 = var4.mAdapter.isEmpty();
			byte var7 = 0;
			if (var6) {
				var7 = 8;
			}

			var5.setVisibility(var7);
		}
	}

	private static class ImplOld extends ActivityContacts.Impl {

		private ImplOld() {
		}

		// $FF: synthetic method
		ImplOld(Object var1) {
			this();
		}

		Bitmap loadContactPhoto(Context param1, long param2, Options param4) {
			// $FF: Couldn't be decompiled
		}
	}

	static final class ContactListItemCache {

		public View divider;
		public View header;
		public TextView headerText;
		public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
		public TextView nameView;
		public ImageView nonQuickContactPhotoView;
		public ImageView selectedView;

	}

	static final class PhotoInfo {

		public long photoId;
		public int position;

		public PhotoInfo(int var1, long var2) {
			this.position = var1;
			this.photoId = var2;
		}
	}

	private class ImageFetchHandler extends Handler {

		private ImageFetchHandler() {
		}

		// $FF: synthetic method
		ImageFetchHandler(Object var2) {
			this();
		}

		public void clearImageFecthing() {
			this.removeMessages(1);
		}

		public void handleMessage(Message param1) {
			// $FF: Couldn't be decompiled
		}
	}

	abstract static class Impl {

		static Uri CONTACTS_CONTENT_FILTER_URI;
		static Uri CONTACTS_CONTENT_URI;
		static String[] CONTACTS_PROJECTION;
		static String CONTACTS_SELECTION;
		static String NAME_COLUMN;
		static final int SUMMARY_ID_COLUMN_INDEX = 0;
		static final int SUMMARY_NAME_COLUMN_INDEX = 1;
		static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 2;

		static ActivityContacts.Impl getImpl() {
			return (ActivityContacts.Impl) (Integer.parseInt(VERSION.SDK) < 5 ? new ActivityContacts.ImplOld(null)
					: new ActivityContacts.ImplNew(null));
		}

		abstract Bitmap loadContactPhoto(Context var1, long var2, Options var4);
	}

	private static class ImplNew extends ActivityContacts.Impl {

		static {
			if ("amazon".equalsIgnoreCase(Build.BRAND) && Build.MODEL.startsWith("KF")) {
				CONTACTS_SELECTION = null;
			} else {
				CONTACTS_SELECTION = "in_visible_group=1";
			}
		}

		private ImplNew() {
		}

		// $FF: synthetic method
		ImplNew(Object var1) {
			this();
		}

		Bitmap loadContactPhoto(Context param1, long param2, Options param4) {
			// $FF: Couldn't be decompiled
		}
	}

	private class ImageDbFetcher implements Runnable {

		private ImageView mImageView;
		long mPhotoId;

		public ImageDbFetcher(long var2, ImageView var4) {
			this.mPhotoId = var2;
			this.mImageView = var4;
		}

		public void run() {
			if (!ActivityContacts.this.isFinishing() && !Thread.interrupted()) {
				Bitmap var2;
				label19: {
					Bitmap var6;
					try {
						var6 = ActivityContacts.this.impl.loadContactPhoto(ActivityContacts.this, this.mPhotoId,
								(Options) null);
					} catch (Exception var7) {
						var7.printStackTrace();
						App.reportThrowable(var7);
						var2 = null;
						break label19;
					}

					var2 = var6;
				}

				if (var2 != null) {
					ActivityContacts.super.mBitmapCache.put(Long.valueOf(this.mPhotoId), new SoftReference(var2));
					if (!Thread.interrupted()) {
						Message var4 = new Message();
						var4.what = 1;
						var4.obj = this.mImageView;
						ActivityContacts.super.mHandler.sendMessage(var4);
						return;
					}
				}
			}

		}
	}

	private final class ContactItemListAdapter extends ResourceCursorAdapter implements OnScrollListener,
			SectionIndexer {

		private static final int FETCH_IMAGE_MSG = 1;
		private String mAlphabet;
		private HashMap mBitmapCache = null;
		private ActivityContacts.ImageFetchHandler mHandler = new ActivityContacts.ImageFetchHandler(null);
		private ActivityContacts.ImageDbFetcher mImageFetcher;
		private SectionIndexer mIndexer;
		private HashSet mItemsMissingImages = null;
		private boolean mLoading = true;
		private int[] mSectionPositions;
		private CharSequence mUnknownNameText;

		public ContactItemListAdapter(Context var2) {
			super(var2, 2130903044, (Cursor) null, false);
			this.mAlphabet = var2.getString(2131099869);
			this.mUnknownNameText = var2.getText(17039374);
			this.setViewResource(2130903044);
			this.mBitmapCache = new HashMap();
			this.mItemsMissingImages = new HashSet();
		}

		// $FF: synthetic method
		static HashSet access$800(ActivityContacts.ContactItemListAdapter var0) {
			return var0.mItemsMissingImages;
		}

		private void bindSectionHeader(View var1, int var2) {
			ActivityContacts.ContactListItemCache var3 = (ActivityContacts.ContactListItemCache) var1.getTag();
			int var4 = this.getSectionForPosition(var2);
			if (this.getPositionForSection(var4) == var2) {
				String var5 = this.mIndexer.getSections()[var4].toString().trim();
				if (!TextUtils.isEmpty(var5)) {
					var3.headerText.setText(var5);
					var3.header.setVisibility(0);
				} else {
					var3.header.setVisibility(8);
				}
			} else {
				var3.header.setVisibility(8);
			}

			if (-1 + this.getPositionForSection(var4 + 1) == var2) {
				var3.divider.setVisibility(8);
			} else {
				var3.divider.setVisibility(0);
			}
		}

		private SectionIndexer getNewIndexer(Cursor var1) {
			return new AlphabetIndexer(var1, 1, this.mAlphabet);
		}

		private void processMissingImageItems(AbsListView var1) {
			Iterator var2 = this.mItemsMissingImages.iterator();

			while (var2.hasNext()) {
				this.sendFetchImageMessage((ImageView) var2.next());
			}

		}

		private void sendFetchImageMessage(ImageView param1) {
			// $FF: Couldn't be decompiled
		}

		private void updateIndexer(Cursor var1) {
			if (this.mIndexer == null) {
				this.mIndexer = this.getNewIndexer(var1);
			} else if (this.mIndexer instanceof AlphabetIndexer) {
				((AlphabetIndexer) this.mIndexer).setCursor(var1);
			} else {
				this.mIndexer = this.getNewIndexer(var1);
			}

			int var2 = this.mIndexer.getSections().length;
			if (this.mSectionPositions == null || this.mSectionPositions.length != var2) {
				this.mSectionPositions = new int[var2];
			}

			for (int var3 = 0; var3 < var2; ++var3) {
				this.mSectionPositions[var3] = -1;
			}

		}

		public boolean areAllItemsEnabled() {
			return false;
		}

		public void bindView(View var1, Context var2, Cursor var3) {
			ActivityContacts.ContactListItemCache var4 = (ActivityContacts.ContactListItemCache) var1.getTag();
			var3.copyStringToBuffer(1, var4.nameBuffer);
			int var5 = var4.nameBuffer.sizeCopied;
			if (var5 != 0) {
				var4.nameView.setText(var4.nameBuffer.data, 0, var5);
			} else {
				var4.nameView.setText(this.mUnknownNameText);
			}

			ImageView var6 = var4.selectedView;
			byte var7;
			if (ActivityContacts.this.mListView.isItemChecked(var3.getPosition())) {
				var7 = 0;
			} else {
				var7 = 8;
			}

			var6.setVisibility(var7);
			ImageView var8 = var4.nonQuickContactPhotoView;
			var4.nonQuickContactPhotoView.setVisibility(0);
			int var9 = var3.getPosition();
			long var10 = 0L;
			if (!var3.isNull(2)) {
				var10 = var3.getLong(2);
			}

			var8.setTag(new ActivityContacts.PhotoInfo(var9, var10));
			if (var10 == 0L) {
				var8.setImageResource(2130837533);
			} else {
				SoftReference var12 = (SoftReference) this.mBitmapCache.get(Long.valueOf(var10));
				Bitmap var13 = null;
				if (var12 != null) {
					var13 = (Bitmap) var12.get();
					if (var13 == null) {
						this.mBitmapCache.remove(Long.valueOf(var10));
					}
				}

				if (var13 != null) {
					var8.setImageBitmap(var13);
					return;
				}

				var8.setImageResource(2130837533);
				this.mItemsMissingImages.add(var8);
				if (ActivityContacts.this.mScrollState != 2) {
					this.sendFetchImageMessage(var8);
					return;
				}
			}

		}

		public void changeCursor(Cursor var1) {
			super.changeCursor(var1);
			this.updateIndexer(var1);
		}

		public void clearImageFetching() {
			// $FF: Couldn't be decompiled
		}

		public int getPositionForSection(int var1) {
			int var2;
			if (var1 >= 0 && var1 < this.mSectionPositions.length) {
				if (this.mIndexer == null) {
					Cursor var5 = ActivityContacts.this.mAdapter.getCursor();
					if (var5 == null) {
						return 0;
					}

					this.mIndexer = this.getNewIndexer(var5);
				}

				var2 = this.mSectionPositions[var1];
				if (var2 == -1) {
					int[] var3 = this.mSectionPositions;
					int var4 = this.mIndexer.getPositionForSection(var1);
					var3[var1] = var4;
					return var4;
				}
			} else {
				var2 = -1;
			}

			return var2;
		}

		public int getSectionForPosition(int var1) {
			int var2 = 0;
			int var3 = this.mSectionPositions.length;

			while (var2 != var3) {
				int var4 = var2 + (var3 - var2) / 4;
				if (this.getPositionForSection(var4) <= var1) {
					var2 = var4 + 1;
				} else {
					var3 = var4;
				}
			}

			return var2 - 1;
		}

		public Object[] getSections() {
			return this.mIndexer.getSections();
		}

		public View getView(int var1, View var2, ViewGroup var3) {
			Cursor var4 = this.getCursor();
			if (!var4.moveToPosition(var1)) {
				throw new IllegalStateException("couldn\'t move cursor to position " + var1);
			} else {
				View var5;
				if (var2 == null) {
					var5 = this.newView(ActivityContacts.this, var4, var3);
				} else {
					var5 = var2;
				}

				this.bindView(var5, ActivityContacts.this, var4);
				this.bindSectionHeader(var5, var1);
				return var5;
			}
		}

		public boolean isEmpty() {
			return this.mLoading ? false : super.isEmpty();
		}

		public View newView(Context var1, Cursor var2, ViewGroup var3) {
			View var4 = super.newView(var1, var2, var3);
			ActivityContacts.ContactListItemCache var5 = new ActivityContacts.ContactListItemCache();
			var5.header = var4.findViewById(2131230732);
			var5.headerText = (TextView) var4.findViewById(2131230733);
			var5.divider = var4.findViewById(2131230737);
			var5.nameView = (TextView) var4.findViewById(2131230736);
			var5.nonQuickContactPhotoView = (ImageView) var4.findViewById(2131230735);
			var5.selectedView = (ImageView) var4.findViewById(2131230734);
			var4.setTag(var5);
			return var4;
		}

		protected void onContentChanged() {
			CharSequence var1 = ActivityContacts.this.mListView.getTextFilter();
			if (!TextUtils.isEmpty(var1)) {
				this.getFilter().filter(var1);
			} else {
				ActivityContacts.this.startQuery();
			}
		}

		public void onScroll(AbsListView var1, int var2, int var3, int var4) {
		}

		public void onScrollStateChanged(AbsListView var1, int var2) {
			ActivityContacts.this.mScrollState = var2;
			if (var2 == 2) {
				this.clearImageFetching();
			} else {
				this.processMissingImageItems(var1);
			}
		}

		public Cursor runQueryOnBackgroundThread(CharSequence var1) {
			return ActivityContacts.this.doFilter(var1.toString());
		}

		public void setLoading(boolean var1) {
			this.mLoading = var1;
		}
	}
}
